﻿using SeaBattle.Domain.Abstract;
using SeaBattle.Domain.Entities;
using SeaBattle.Domain.Abstract.Services;
using System.Collections.Generic;
using System.Linq;
using System;

namespace SeaBattle.Domain.Concrete
{
    public class FakePlayerRepository: IPlayerRepository
    {
        public FakePlayerRepository()
        {
            if (players.Count == 0)
                InitializePlayers();  
        }

        private static List<Player> players = new List<Player>();

        public Player CreateNewPlayer(string name, string login, string password)
        {
            return new Player(name, login,password);                    
        }

        public void SavePlayer(Player player)
        {
            if (!players.Contains(player))
                players.Add(player);
        }

        private void InitializePlayers()
        {
            players.Add(new Player("Василий Пупкин", "vasya", "111"));
            players.Add(new Player("Александр Македонский","alex","111"));
            players.Add(new Player("Андрей Шевченко","andrew","111"));
        }
        /// <summary>
        /// Gets the player.
        /// </summary>
        /// <param name="login">The login.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public Player GetPlayer(string login)
        {
            var player = players.FirstOrDefault(x => x.Login == login);
            if (player == null)
            {
                throw new IndexOutOfRangeException("Player with login " + login + " doesnt exists");
            }
            return player;
        }


        public IList<Player> Players
        {
            get { return players.AsReadOnly(); }
        }
    }
}
